package com.qili.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qili.core.utils.DictTypes;
import com.qili.core.utils.DictUtil;
import com.qili.entity.ReDTO;
import com.qili.entity.affaircase.AffairRecord;
import com.qili.entity.affaircase.dto.AffairCaseDTO;
import com.qili.entity.base.BaseCompany;
import com.qili.entity.base.BaseCompanyOwner;
import com.qili.entity.base.BaseDict;
import com.qili.entity.supervisor.ProjectNotice;
import com.qili.entity.supervisor.SupervisorFile;
import com.qili.entity.supervisor.SupervisorFileTemp;
import com.qili.entity.supervisor.SupervisorProjectModifiedRecord;
import com.qili.entity.supplier.*;
import com.qili.entity.supplier.dto.*;
import com.qili.entity.sys.*;
import com.qili.entity.sys.dto.SysRoleUserDTO;
import com.qili.entity.sys.dto.SysUserDTO;
import com.qili.entity.zx.dto.ZxProjectDTO;
import com.qili.exception.MyException;
import com.qili.service.SysUserService;
import com.qili.service.affairCase.AffairCaseService;
import com.qili.service.affairCase.AffairRecordService;
import com.qili.service.base.BaseCompanyOwnerService;
import com.qili.service.base.BaseCompanyService;
import com.qili.service.base.BaseDictService;
import com.qili.service.supervisor.*;
import com.qili.service.supplier.*;
import com.qili.service.supplier.ProjectPlaceService;
import com.qili.service.sys.*;
import com.qili.util.*;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.*;

/**
 * @author zhaoyongke
 * @date 2021/10/18
 * @description
 */

@Controller
@Slf4j
@RequestMapping("/app/gx")
public class AppGxController {

    @Autowired
    SysUserService sysUserService;

    @Autowired
    SupplierProjectManagerService supplierProjectManagerService;
    @Autowired
    SysRegionService sysRegionService;
    @Autowired
    BaseDictService baseDictService;
    @Autowired
    BaseCompanyService baseCompanyService;
    @Autowired
    ProjectPlaceService projectPlaceService;
    @Autowired
    OperQueryRecordService operQueryRecordService;
    @Autowired
    AffairCaseService affairCaseService;


    @Autowired
    SupplierProjectManagerChangeService supplierProjectManagerChangeService;
    @Autowired
    ProjectPlaceChangeService projectPlaceChangeService;

    @Autowired
    SupplierProjectAcceptService supplierProjectAcceptService;

    @Autowired
    SupervisorFileTempService supervisorFileTempService;

    @Value("${supervisor.tempPath}")
    private String tempPath;

    @Autowired
    SupervisorFileService supervisorFileService;

    @Autowired
    SupervisorProjectModifiedRecordService supervisorProjectModifiedRecordService;
    @Autowired
    SysUserWorkExperienceService userWorkExperienceService;
    @Autowired
    ProjectNoticeServie projectNoticeServie;

    @Autowired
    AffairRecordService affairRecordService;
    @Autowired
    ProjectCollectService projectCollectService;
    @Autowired
    SupervisorProjectService supervisorProjectService;
    @Autowired
    OaDataService oaDataService;
    @Autowired
    private SysUserProjectService sysUserProjectService;
    @Autowired
    private SysUserCertificateService userCertificateService;

    @Autowired
    UploadUtil uploadUtil;

    @Autowired
    private SysUserEducationService userEducationService;

    @Autowired
    private SupplierProjectPubNoticeService supplierProjectPubNoticeService;
    @Autowired
    private BaseCompanyOwnerService baseCompanyOwnerService;

    @Autowired
    SupervisorProjectModifiedRecordService modifiedRecordService;

    @Autowired
    ProjectCollectService pcService;


    /**
     * @param project
     * @Author:zhaoyongke
     * @Description: 首页--项目列表
     * @Date:16:45 2021/3/13
     */
    @ApiOperation(value = "/showProjectList", httpMethod = "GET", notes = "展示项目")
    @PostMapping(value = "showProjectList")
    @ResponseBody
    public JsonUtil showProjectList(@RequestBody SupplierProjectDTO project) {
        try {
            //已发布
            project.setStatus("006");
            String projectName = project.getProjectName();
            String userId = project.getUserId();
            if (StringUtils.isBlank(userId)) {
                return JsonUtil.error("参数获取失败");

            }
            OperQueryRecord record = new OperQueryRecord();
//        if(StringUtils.isNotBlank(projectName)){
//            record.setKeyword(projectName);
//            operQueryRecordService.insertSelective(record);
//        }
            // 判断当前登录人 是内部 还是外部
            SysUser sysUser = getSysUser(userId);
            String source = sysUser.getSource();
            String gxType = sysUser.getGxType();
            String personType = sysUser.getPersonType();

            SysRoleUserDTO sysRoleUserDTO = new SysRoleUserDTO();
            sysRoleUserDTO.setUserId(userId);
            List<SysRoleUserDTO> ListsysRoleUserDTOS = sysUserService.selectRoleUserByUser(sysRoleUserDTO);
            Boolean isfind = false;
            Boolean  scbjl=false;
            Boolean zbCheckPerson=false;
            if (ListsysRoleUserDTOS != null) {
                if (ListsysRoleUserDTOS.size() > 0) {
                    for (int i = 0; i < ListsysRoleUserDTOS.size(); i++) {
                        SysRoleUserDTO sysRoleUserDTO1 = ListsysRoleUserDTOS.get(i);
                        String roleName = sysRoleUserDTO1.getRoleName();
                        if ("admin".equals(roleName) || "gsgc".equals(roleName) || "zbCheckPerson".equals(roleName) || "pucheckper".equals(roleName)) {
                            isfind = true;
                        }
                        if("scbjl".equals(roleName)){
                            scbjl=true;
                        }
                        if("zbCheckPerson".equals(roleName)){
                            zbCheckPerson=true;
                        }
                    }
                }

            }
            if (!isfind) {
                if (!"001".equals(source)) {
                    //外部人员 只能查看正中内部的  为公开状态的
                    project.setViewType("11111");
                }
                if (StringUtils.isNotBlank(gxType) && StringUtils.isNotBlank(personType) && "001".equals(gxType) && "002".equals(personType)) {
                    project.setPublishTypes("001");
                }
            }
            if (zbCheckPerson  ||  scbjl) {
                // 中标审核人 可以看到 已过时间的
                project.setZbcheckview("1");
                if(scbjl){
                    // 2021 1111 市场部经理 能查看到 所有的  scbjl
                    project.setDestatus("001");
                    project.setStatus(null);
                }
            } else {
                project.setZbcheckview("0");
            }
            List<SupplierProjectDTO> supplierProjectDTOS = supplierProjectManagerService.selectListApp(project);
            return JsonUtil.sucessWithData("返回列表成功", supplierProjectDTOS);

        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.error("获取失败");
        }

    }


    /**
     * @param project
     * @Author:zhaoyongke
     * @Description:项目列表 我的项目
     * @Date:18:16 2021/3/25
     */
    @ApiOperation(value = "/myProjectList", httpMethod = "GET", notes = "展示项目")
    @PostMapping(value = "/myProjectList")
    @ResponseBody
    public JsonUtil myProjectList(@RequestBody SupplierProjectDTO project) {
        HashMap map = new HashMap();
        try {
            String userId = project.getUserId();
            SysUser sysUser = getSysUser(userId);
            //001 供方 002 需方
            String gxType = sysUser.getGxType();
            //需方展示自己发布的所有的项目，供方展示所有应接的项目。
            if ("002".equals(gxType)) {
                project.setCreateBy(userId);
            } else if ("001".equals(gxType)) {
                //
                project.setAcceptBy(userId);
            }
            List<SupplierProjectDTO> list = supplierProjectManagerService.selectMyProjectListApp(project);

            return JsonUtil.sucessWithData("返回列表成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.error("获取失败");
        }


    }

    public SysUser getSysUser(String userId) {
        SysUser user = sysUserService.selectByPrimaryKey(userId);
        return user;
    }


    /**
     * @Author:zhaoyongke
     * @Description:项目详情
     * @Date:18:05 2021/3/22
     */
    @PostMapping(value = "/projectDetail")
    @ResponseBody
    public JsonUtil projectDetail(@RequestBody SupplierProjectDTO supplierProjectDTO, Model model, HttpServletRequest request) {
        try {
            HashMap map = new HashMap();
            String userId = supplierProjectDTO.getUserId();
            if (StringUtils.isBlank(userId)) {
                return JsonUtil.error("参数获取失败");
            }
//        Subject subject = SecurityUtils.getSubject();
//        if (subject.hasRole("admin") || subject.hasRole("gsgc") || subject.hasRole("zbCheckPerson") || subject.hasRole("pucheckper")) {
//            model.addAttribute("admin","admin");
//        }

//        String type = request.getParameter("type");
//        // 外部單位发布的项目 x天无应接
//        String fix = request.getParameter("fix");
        // 内部单位中标审核人 获取的参数  消息类型107 ====
        String zbshr = supplierProjectDTO.getZbshr();
        String tjperson =supplierProjectDTO.getTjperson();
        String caseId =supplierProjectDTO.getCaseId();
        //----------------
//        if (StringUtil.isNotBlank(fix)) {
//            model.addAttribute("fix", "1");
//        }
            // 外部单位发布的项目--推荐确认
        String tjconfirm = supplierProjectDTO.getTjconfirm();
        if (StringUtil.isNotBlank(tjconfirm)) {
            map.put("tjconfirm","1");
            //model.addAttribute("tjconfirm", "1");
        }
        if (StringUtil.isNotBlank(caseId)) {
           // 处理消息 为已看
            ProjectNotice projectNotice = projectNoticeServie.selectByPrimaryKey(caseId);
            projectNotice.setIsView("1");
            projectNoticeServie.updateByPrimaryKey(projectNotice);
            //从 消息中过来的 详情
            map.put("fromnotice","1");
//            model.addAttribute("fromnotice", "1");
        }
//        String contract = request.getParameter("contract");
//        if ("s".equals(contract)) {
//            model.addAttribute("contract", "s");
//        }
            String projectId = supplierProjectDTO.getSupProjectId();
            SupplierProjectDTO supplierProjectDTO1 = new SupplierProjectDTO();
            supplierProjectDTO1.setSupProjectId(projectId);
            List<SupplierProjectDTO> supplierProjectDTOS = supplierProjectManagerService.selectProjectApp(supplierProjectDTO1);
            String publishType = "";
            SupplierProjectDTO projectDTO = new SupplierProjectDTO();
            if (supplierProjectDTOS.size() > 0) {
                projectDTO = supplierProjectDTOS.get(0);
                String checkStatus = projectDTO.getCheckStatus();
                String status = projectDTO.getStatus();
                String publishBy = projectDTO.getPublishBy();
                String createBy = projectDTO.getCreateBy();
                publishType = projectDTO.getPublishType();
                String pubComType = projectDTO.getPubComType();
                String zbCompany = projectDTO.getZbCompany();
                String zbComType = projectDTO.getZbComType();
                // 中标人
                String zbBy = projectDTO.getZbBy();
                //            if(StringUtil.isNotBlank(zbBy)){
                //                String userId1 = CommonUtil.getUserId();
                //                if(userId1.equals(zbBy)){
                //                    model.addAttribute("zbperson","1");
                //                }
                //            }
                if ("001".equals(pubComType)) {
                    BaseCompany baseCompany = baseCompanyService.selectByPrimaryKey(projectDTO.getPublishCompany());
                    projectDTO.setPublishCompany(baseCompany.getBcName());
                    map.put("comName",baseCompany.getBcName());
                    // model.addAttribute("comName", baseCompany.getBcName());
                } else {
                    SysUser sysUser = sysUserService.selectByPrimaryKey(projectDTO.getPublishCompany());
                    projectDTO.setPublishCompany(sysUser.getCompanyName());
                    map.put("comName",sysUser.getCompanyName());
                    //                model.addAttribute("comName", sysUser.getCompanyName());
                }
                //获取中标单位
               if("001".equals(zbComType)){
                   BaseCompany baseCompany = baseCompanyService.selectByPrimaryKey(zbCompany);
                   map.put("zbComName",baseCompany.getBcName());
                   // model.addAttribute("zbComName", baseCompany.getBcName());

               }else if("002".equals(zbComType)){
                                SysUser sysUser = sysUserService.selectByPrimaryKey(zbCompany);
                   map.put("zbComName",sysUser.getCompanyName());
//                                model.addAttribute("zbComName", sysUser.getCompanyName());
                 }
                if ("006".equals(status) && (userId.equals(publishBy) || userId.equals(createBy)) && ("1".equals(checkStatus) || "0".equals(checkStatus))) {
                    //已经发布的项目  发布人和创建人 和审核人看到的最新修改过的项目
                    //审核中---查询变更表
                    SupplierProjectChangeDTO changeDTO = supplierProjectManagerChangeService.getChangeProject(projectDTO);
                    map.put("pro", changeDTO);
                    //                model.addAttribute("project", changeDTO);
                    //                //处理实施地点和人员地点要求
                    //                getProjectPlaceChange(model, projectDTO.getSupProjectId(), changeDTO.getSupProjectChangeId());
                    SupplierProjectDTO dicprodto = new SupplierProjectDTO();
                    BeanUtil.copyNotNullBean(changeDTO, dicprodto);
                    //                //处理字典
                    getProjectDic(map, dicprodto);
                    //                // 获取文件
                    getFileModel(map, changeDTO.getSupProjectChangeId());

                } else {
                    map.put("pro", projectDTO);
                    //                model.addAttribute("project", projectDTO);
                    //处理实施地点和人员地点要求
                    getProjectPlace(map, projectDTO);
                    //                //处理字典
                    getProjectDic(map, projectDTO);
                    //                // 获取文件
                    getFileModel(map, projectDTO.getSupProjectId());
                }

                // 实施比例
                List<BaseDict> typeListSsbl = baseDictService.selectByTypeCode(DictTypes.SSBL.getType());
                if (typeListSsbl.size() > 0) {
                    BaseDict ssbl = typeListSsbl.get(0);
                    String ssblper = ssbl.getBadtLevel1();
                    model.addAttribute("ssblper", ssblper);
                }
            } else {
                //            model.addAttribute("message","该项目已删除");
                //            return "/error/message";
            }
            //收藏按钮展示
//        ProjectCollect pc=new ProjectCollect();
//        pc.setProjectId(projectId);
//        pc.setCreateBy(userId);
//        List<ProjectCollect> list=projectCollectService.selectCollect(pc);
//        if(list!=null&&list.size()>0){
//            model.addAttribute("collect","1");
//        }else{
//            model.addAttribute("collect","0");
//        }
            addModelDetail(model, projectDTO,map,userId);
//        // 内部项目推荐
        if (StringUtil.isNotBlank(zbshr)) {
            // 消息类型 处理消息类型 107
            map.put("zbshr","1");
            map.put("tjperson",tjperson);
            map.put("acceptview","1");
            map.put("gxtype","x");
        }
            // 内部项目 推荐中标审核人  通过列表 也能 显示 推荐 同意 或 不同意
//            Example enb = new Example(ProjectNotice.class);
//            Example.Criteria cnb = enb.createCriteria();
//            cnb.andEqualTo("linkId", supplierProjectDTO.getSupProjectId());
//            cnb.andEqualTo("sendTo", CommonUtil.getUserId());
//            cnb.andEqualTo("noticeType", "107");
//            List<ProjectNotice> projectNoticesnb = projectNoticeServie.selectByExample(enb);
//            if (projectNoticesnb != null) {
//                if (projectNoticesnb.size() > 0) {
//                    for (int i = 0; i < projectNoticesnb.size(); i++) {
//                        ProjectNotice notice = projectNoticesnb.get(i);
//                        String tjPerson = notice.getTjPerson();
//                        model.addAttribute("tjperson", tjPerson);
//                    }
//                }
//            }
//        boolean istjperson = model.containsAttribute("tjperson");
//        if(subject.hasRole("zbCheckPerson")  &&  istjperson){
//            model.addAttribute("zbshr", "1");
//            model.addAttribute("acceptview", "1");
//            model.addAttribute("gxtype", "x");
//        }

            // 外部发布的项目--推荐确认
        if (StringUtil.isNotBlank(tjconfirm)) {
            map.put("tjconfirm","1");
            map.put("tjperson",tjperson);
//            model.addAttribute("tjconfirm", "1");
//            model.addAttribute("tjperson", tjperson);
        }
            // 外部发布的项目--推荐确认 ----外部查询项目查询推荐 解决 推荐按钮不能通过   项目列表 点击显示
//        Example e = new Example(ProjectNotice.class);
//        Example.Criteria c = e.createCriteria();
//        c.andEqualTo("linkId", supplierProjectDTO.getSupProjectId());
//        c.andEqualTo("sendTo", CommonUtil.getUserId());
//        c.andEqualTo("noticeType", "105");
//        List<ProjectNotice> projectNotices = projectNoticeServie.selectByExample(e);
//        if(projectNotices!=null){
//            if(projectNotices.size()>0){
//                for (int i = 0; i <projectNotices.size() ; i++) {
//                    ProjectNotice notice = projectNotices.get(i);
//                    String tjPerson = notice.getTjPerson();
//                    model.addAttribute("tjperson",tjPerson);
//                    model.addAttribute("tjconfirm", "1");
//                }
//            }
//        }
//        // 项目归属
//        List<BaseCompanyOwner> alstCompanyGs = baseCompanyOwnerService.selectAll();
//        model.addAttribute("comlistOwner", alstCompanyGs);
//        return url;


            // 变更
            SupplierProjectChange change = new SupplierProjectChange();
            change.setSupProjectId(supplierProjectDTO.getSupProjectId());
            change.setCheckStatus("2");
            List<SupplierProjectChange> selectchange = supplierProjectManagerChangeService.select(change);
            List cahngelist = new ArrayList();
            if (selectchange != null && selectchange.size() > 0) {
                for (int i = 0; i < selectchange.size(); i++) {
                    SupplierProjectChange supplierProjectChange = selectchange.get(i);
                    String supProjectChangeId = supplierProjectChange.getSupProjectChangeId();
                    HashMap mapcahnge = new HashMap();
                    HashMap changeDetail = getChangeDetail(supProjectChangeId, mapcahnge);
                    cahngelist.add(changeDetail);
                }
            }

            map.put("changelist", cahngelist);
            // 结果公示
            JsonUtil jggs = getJggs(projectDTO);
            Object data = jggs.getData();
            map.put("gsdetail", data);
            // 查询表更记录


            SupplierProjectAcceptDTO acceptDTO = new SupplierProjectAcceptDTO();
            acceptDTO.setSupProjectId(supplierProjectDTO.getSupProjectId());
            acceptDTO.setAcceptOrder("1");
            SysUser sysUser = getSysUser(userId);
            if ("001".equals(sysUser.getGxType())) {
                //供方显示我的迎接
                acceptDTO.setAcceptBy(userId);
            }
            // 如果是中标审核人 和 发布审核人  等看到所有的应接人
            if (StringUtil.isNotBlank(zbshr) || StringUtil.isNotBlank(tjconfirm) ) {
                acceptDTO.setAcceptBy(null);
            }
            List<SupplierProjectAcceptDTO> supplierProjectAcceptDTOS = supplierProjectAcceptService.selectListAcept(acceptDTO);
            if(supplierProjectAcceptDTOS!=null  &&  supplierProjectAcceptDTOS.size()>0){
                for (int i = 0; i <supplierProjectAcceptDTOS.size() ; i++) {
                    // 查询投标文件
                    SupplierProjectAcceptDTO acceptDTO1 = supplierProjectAcceptDTOS.get(i);
                    SupervisorFileTemp supervisorFileTemp = new SupervisorFileTemp();
                    supervisorFileTemp.setLinkId(acceptDTO1.getSupAcceptId());
                    List<SupervisorFileTemp> selectfile = supervisorFileTempService.select(supervisorFileTemp);
                    supplierProjectAcceptDTOS.get(i).setFilelist(selectfile);
                }
            }
            map.put("acceptlist", supplierProjectAcceptDTOS);
            // 发布类型
            map.put("publishType",publishType);
            map.put("user",sysUser);
            return JsonUtil.sucessWithData("返回列表成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.error("参数失败");
        }

    }

    /**
     * @param dto
     * @Author:zhaoyongke
     * @Description: 中标审核人推荐中标
     * @Date:16:56 2021/4/13
     */
    @PostMapping("/saveUpdateTjZb")
    @ResponseBody
    public JsonUtil saveUpdateTjZb(@RequestBody SupplierProjectDTO dto) {
        if (dto != null) {
            try {
                JsonUtil re = new JsonUtil();
                String proId = dto.getSupProjectId();
                String checkstatus = dto.getCheckStatus();
                String tjPerson = dto.getTjperson();
                String userId = dto.getUserId();
                if (StringUtil.isBlank(proId) || StringUtil.isBlank(checkstatus) || StringUtil.isBlank(tjPerson)  || StringUtil.isBlank(userId)) {
                    re.setFlag(false);
                    re.setMsg("获取参数失败");
                    return re;
                }
                SupplierProject project = supplierProjectManagerService.selectByPrimaryKey(proId);
                if (!"006".equals(project.getStatus())) {
                    re.setFlag(false);
                    re.setMsg("只能操作已发布状态的项目");
                    return re;
                }

                AffairRecord recordquery = new AffairRecord();
                recordquery.setYwType("000");
                recordquery.setOperUserId(userId);
                recordquery.setYwId(dto.getSupProjectId());
                List<AffairRecord> select = affairRecordService.select(recordquery);
                if (select.size() > 0) {
                    // 更新操作
                    AffairRecord record = select.get(0);
                    record.setCheckStatus(dto.getCheckStatus());
                    affairRecordService.updateByPrimaryKey(record);
                } else {
                    AffairRecord record = new AffairRecord();
                    record.setCheckStatus(dto.getCheckStatus());
                    //
                    record.setYwId(dto.getSupProjectId());
                    // 存入推荐人
                    record.setTaskId(dto.getTjperson());
                    record.setYwId(dto.getSupProjectId());
                    record.setOperUserId(userId);
                    record.setOperDate(DateUtil.getCurrentDateString());
                    record.setYwType("000");
                    record.setRemark(dto.getRemark());
                    affairRecordService.insert(record);
                }
                // 判断所有的中标审核人是否都审核过
                AffairRecord recordquerycheck = new AffairRecord();
                recordquerycheck.setYwType("000");
                recordquerycheck.setYwId(dto.getSupProjectId());
                recordquerycheck.setCheckStatus("2");
                List<AffairRecord> selectcheck = affairRecordService.select(recordquerycheck);
                SysUserDTO sysUserDTO = new SysUserDTO();
                sysUserDTO.setUserRole("zbCheckPerson");
                List<SysUser> sysUsers = sysUserService.getUserDicts(sysUserDTO);

                if (sysUsers.size() == selectcheck.size()) {
                    // 都审核完毕
                    AffairRecord record = selectcheck.get(0);
                    //意见一致 审核通过 更新中标结果
                    String ywId = record.getYwId();
                    String tjzbperson = record.getTaskId();
                    //根据推荐人 更新中标结果
                    updateZb(ywId, tjzbperson, "002");
                    //项目中标人确定后推送消息给发布人：您发布的XXX项目中标人为XXX。
                    // 给需方发送消息
                    //您发布的XXX项目中标人为XXX。
                    sendNoticeToXfZb(ywId, tjzbperson,"内部发布的项目--推荐中标后中标审核人都同意推荐结果");
                    re.setData("1");

                }
                SupplierProject projectnew = supplierProjectManagerService.selectByPrimaryKey(proId);
                projectnew.setIsConfirm(checkstatus);
                supplierProjectManagerService.updateByPrimaryKey(projectnew);
                re.setFlag(true);
                re.setMsg("操作成功");
                return re;
            } catch (Exception e) {
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return JsonUtil.error("操作失败:" + e.getMessage());
            }

        } else {
            return JsonUtil.error("获取数据失败！！");
        }


    }

    /**
     * @param dto
     * @Author:zhaoyongke
     * @Description:
     * @Date:17:41 2021/4/15
     */
    @PostMapping("/saveWbUpdateTjZb")
    @ResponseBody
    public JsonUtil saveWbUpdateTjZb(@RequestBody SupplierProjectDTO dto) {
        if (dto != null) {
            try {
                JsonUtil re = new JsonUtil();
                String proId = dto.getSupProjectId();
                String checkstatus = dto.getCheckStatus();
                String tjPerson = dto.getTjperson();
                if (StringUtil.isBlank(proId) || StringUtil.isBlank(checkstatus) || StringUtil.isBlank(tjPerson)) {
                    re.setFlag(false);
                    re.setMsg("获取参数失败");
                    return re;
                }
                SupplierProject project = supplierProjectManagerService.selectByPrimaryKey(proId);
                if (!"006".equals(project.getStatus())) {
                    re.setFlag(false);
                    re.setMsg("只能操作已发布状态的项目");
                    return re;
                }
                if ("2".equals(checkstatus)) {
                    //根据推荐人 更新中标结果
                    updateZb(proId, tjPerson, "002");
                    //项目中标人确定后推送消息给发布人：您发布的XXX项目中标人为XXX。
                    // 给需方发送消息
                    //您发布的XXX项目中标人为XXX。
                    sendNoticeToXfZb(proId, tjPerson,"外部发布的项目--需方同意推荐结果选择中标人");
                    // 更新推荐 中标 消息  为 已经处理完成
                    // updateNoticeTj(proId,"105");
                    re.setData("1");
                }
                SupplierProject projectnew = supplierProjectManagerService.selectByPrimaryKey(proId);
                projectnew.setIsConfirm(checkstatus);
                if("3".equals(checkstatus)){
                    projectnew.setRemark(dto.getRemark());
                }
                supplierProjectManagerService.updateByPrimaryKey(projectnew);
                re.setFlag(true);
                re.setMsg("操作成功");
                return re;
            } catch (Exception e) {
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return JsonUtil.error("操作失败:" + e.getMessage());
            }

        } else {
            return JsonUtil.error("获取数据失败！！");
        }


    }

    /**
     * @Author:zhaoyongke
     * @Description: 发送需方--中标消息
     * @Date:14:30 2021/4/7
     */
    public void sendNoticeToXfZb(String projectId, String zbperson,String remark) {
        SupplierProject project = supplierProjectManagerService.selectByPrimaryKey(projectId);
        SysUser sysUser = sysUserService.selectByPrimaryKey(zbperson);
        String personType = sysUser.getPersonType();
        String realName = "";
        if ("001".equals(personType)) {
            // 个人
            realName = sysUser.getRealName();
        } else {
            realName = sysUser.getLinkPerson();
        }
        ProjectNotice notice = new ProjectNotice();
        notice.setLinkId(project.getSupProjectId());
        notice.setIsView("0");
        notice.setSendTo(project.getCreateBy());
        notice.setTitle("您发布的" + project.getProjectName() + "项目中标人为" + realName);
        notice.setNoticeType("108");// 中标人确认消息发送
        notice.setApplicantType("002");// 供需
        notice.setContent(project.getProjectName());
        notice.setCreateDate(DateUtil.getCurrentDateString());
        notice.setRemark(remark);
        projectNoticeServie.insertSelective(notice);
    }


    /**
     * @param projectId
     * @param acceptId
     * @Author:zhaoyongke
     * @Description:
     * @Date:16:45 2021/4/13
     */
    public void updateZb(String projectId, String acceptId, String type) throws Exception {
        String accpt1 = "";
        String time = DateUtil.getCurrentDateTimeString();
        //查询中标公司和未中标公司  更新状态
        SupplierProjectAccept notdto = new SupplierProjectAccept();
        notdto.setSupProjectId(projectId);
        List<SupplierProjectAccept> list = supplierProjectAcceptService.select(notdto);
        if (list != null && list.size() > 0) {
            for (SupplierProjectAccept a : list) {
                if ("001".equals(type)) {
                    if (!acceptId.equals(a.getSupAcceptId())) {
                        a.setIsZb("0");
                        supplierProjectAcceptService.updateByPrimaryKeySelective(a);
                        //给未中标发消息
                        sendNoticeNotZb(a);
                    } else {
                        // 生成账号
                        saveCreateZh(a, projectId);
                        a.setZbDate(time);
                        a.setIsZb("1");
                        supplierProjectAcceptService.updateByPrimaryKeySelective(a);
                        //给中标人发消息
                        sendNoticeZb(a);
                    }
                } else {
                    if (!acceptId.equals(a.getAcceptBy())) {
                        // 推荐的招标人
                        a.setIsZb("0");
                        supplierProjectAcceptService.updateByPrimaryKeySelective(a);
                        //给未中标发消息
                        sendNoticeNotZb(a);
                    } else {
                        accpt1 = a.getSupAcceptId();
                        // 生成账号
                        saveCreateZh(a, projectId);
                        a.setZbDate(time);
                        a.setIsZb("1");
                        supplierProjectAcceptService.updateByPrimaryKeySelective(a);
                        //给中标人发消息
                        sendNoticeZb(a);
                    }

                }

            }
        }
        //更新中标项目
        String accId = acceptId;
        if ("001".equals(type)) {
            accId = acceptId;
        } else {
            accId = accpt1;
        }
        SupplierProjectAccept adto = supplierProjectAcceptService.selectByPrimaryKey(accId);
        SupplierProject project = supplierProjectManagerService.selectByPrimaryKey(projectId);
        project.setZbStatus("1");
        project.setZbCompany(adto.getCompanyId());
        project.setZbDate(time);
        project.setZbComType(adto.getComType());
        project.setZbBy(adto.getAcceptBy());
        project.setSupProjectId(projectId);
        project.setStatus("004");
        supplierProjectManagerService.updateByPrimaryKey(project);
        // 中标后自动公示
        saveGs(project);
    }




    /**
     * @Author:zhaoyongke
     * @Description: 中标以后自动公示
     * @param dto
     * @Date:10:23 2021/9/8
     */
    public   void  saveGs(SupplierProject  dto){

        SupplierProject project = supplierProjectManagerService.selectByPrimaryKey(dto.getSupProjectId());
        SupplierProjectPubNotice  pubNotice=new SupplierProjectPubNotice();
        pubNotice.setSupProjectId(dto.getSupProjectId());
        pubNotice.setPubComType(project.getPubComType());
        pubNotice.setPubCompany(project.getPublishCompany());
        pubNotice.setStartDate(DateUtil.getCurrentDateString());
        String gsDays = dto.getGsDays();
        if(StringUtil.isNotBlank(gsDays)){
            Double aDouble = Double.valueOf(gsDays)*24;
            int  addHours = aDouble.intValue();
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(new Date());
            calendar.add(Calendar.HOUR, addHours);
            Date dateend = new Date(calendar.getTimeInMillis());
            pubNotice.setEndDate(DateUtil.formatDate(dateend,"yyyy-MM-dd HH:mm"));
        }
        /**
         * 公示状态 0  未公示 1 公示中  2 结束
         */
        pubNotice.setGsStatus("1");
        // 中标人
        pubNotice.setZbBy(project.getZbBy());
        pubNotice.setSupProjectId(project.getSupProjectId());
        pubNotice.setZbComType(project.getZbComType());
        pubNotice.setZbCompany(project.getZbCompany());
        supplierProjectPubNoticeService.insertSelective(pubNotice);
        project.setIsGs("1");
        supplierProjectManagerService.updateByPrimaryKey(project);

    }
    public void saveCreateZh(SupplierProjectAccept accept, String projectId) throws Exception {
        //  项目应接人----【供方合同负责人】角色权限
        String acceptBy = accept.getAcceptBy();
        SysUser sysUseraccept = sysUserService.selectByPrimaryKey(acceptBy);
        String personType = sysUseraccept.getPersonType();
        String source = sysUseraccept.getSource();
        //添加默认角色【供方合同负责人】角色权限
        List<String> initRoleList = new ArrayList<String>();
        initRoleList.add("gfhtperson");
        sysUserService.setMenuAndRolesWhenRegist(sysUseraccept, initRoleList);
        //输入的项目经理生成账号 和 权限
        SupplierProject project = supplierProjectManagerService.selectByPrimaryKey(projectId);
        String projectType = project.getProjectType();
        if (projectType.indexOf("001") > -1) {
            //监理
            String jlManagerPerson = accept.getJlManagerPerson();
            if (StringUtil.isBlank(jlManagerPerson)) {
                String jlManagerPersonName = accept.getJlManagerPersonName();
                SysUser sysUser = saveCreateUser(jlManagerPersonName, sysUseraccept, "001", "xmjl");
                accept.setJlManagerPerson(sysUser.getId());
            }

        }
        if (projectType.indexOf("002") > -1) {
            //咨询
            String zxManagerPerson = accept.getZxManagerPerson();
            if (StringUtil.isBlank(zxManagerPerson)) {
                String zxManagerPersonName = accept.getZxManagerPersonName();
                SysUser sysUser = saveCreateUser(zxManagerPersonName, sysUseraccept, "002", "zxxmjl");
                accept.setZxManagerPerson(sysUser.getId());
            }

        }
        if (projectType.indexOf("003") > -1) {
            //检测
            String jcManagerPerson = accept.getJcManagerPerson();
            if (StringUtil.isBlank(jcManagerPerson)) {
                String jcManagerPersonName = accept.getJcManagerPersonName();
                SysUser sysUser = saveCreateUser(jcManagerPersonName, sysUseraccept, "003", "jcxmjl");
                accept.setJcManagerPerson(sysUser.getId());
            }

        }
        if (projectType.indexOf("100") > -1) {
            //其他
            String qtManagerPerson = accept.getQtMagagerPerson();
            if (StringUtil.isBlank(qtManagerPerson)) {
                String qtManagerPersonName = accept.getQtManagerPersonName();
                SysUser sysUser = saveCreateUser(qtManagerPersonName, sysUseraccept, "100", "qtxmjl");
                accept.setQtMagagerPerson(sysUser.getId());
            }

        }


    }

    //给中标人发消息
    public void sendNoticeZb(SupplierProjectAccept supa) {
        SupplierProject sup=supplierProjectManagerService.selectByPrimaryKey(supa.getSupProjectId());
        ProjectNotice notice = new ProjectNotice();
        notice.setLinkId(supa.getSupProjectId());
        notice.setIsView("0");
        notice.setSendTo(supa.getAcceptBy());
        if(sup!=null&&!"".equals(sup)){
            notice.setTitle("恭喜您中标" + sup.getProjectName() + "项目，请及时查看");
        }
        notice.setNoticeType("101");// 公示结束 发送消息
        notice.setApplicantType("002");// 供需
        notice.setCreateDate(DateUtil.getCurrentDateString());
        projectNoticeServie.insertSelective(notice);
    }
    /**
     * @param realName
     * @param sysUseraccept
     * @param ywType
     * @param roleName
     * @Author:zhaoyongke
     * @Description: 创建账号
     * @Date:11:30 2021/4/9
     */
    public SysUser saveCreateUser(String realName, SysUser sysUseraccept, String ywType, String roleName) throws Exception {
        SysUser user = new SysUser();
        user.setUsername(realName);
        if ("001".equals(sysUseraccept.getPersonType())) {
            // 个人
        } else {
            // 组织
            user.setCompanyName(sysUseraccept.getCompanyName());
        }
        user.setSource("002");
        // 供需类型
        user.setGxType("001");
        // 人員类型
        user.setPersonType(sysUseraccept.getPersonType());
        user.setYwType(ywType);
        saveNewUserIdForProjectLeaderAndUnits(user);
        // 赋予角色
        List<String> userRoleNameList = new ArrayList<String>();
        userRoleNameList.add(roleName);
        sysUserService.setMenuAndRolesWhenRegist(user, userRoleNameList);
        return user;
    }


    /**
     * 注册新用户,
     * 查找系统中是否存在同名用户
     * 如果存在同名用户,
     * 如果同一个公司中存在同名用户,给user设置id为该同名用户id
     * 如果同名用户不为同一个公司,那么根据规则生成新用户,给user设置新生成的用户id
     *
     * @param user
     * @return
     */
    public void saveNewUserIdForProjectLeaderAndUnits(SysUser user) throws Exception {
        SysUser sameNameUser = new SysUser();
        sameNameUser.setRealName(user.getUsername());
        //查询同名的用户
        List<SysUser> listuser = sysUserService.select(sameNameUser);
        Integer count = 0;
        if (listuser.size() > 0) {
            //存在相同username的账号
       /*     SysUser realNameUser = new SysUser();
            realNameUser.setRealName(user.getUsername());
            List<SysUser> listuserReal = sysUserService.select(realNameUser);*/
            for (int i = 0; i < listuser.size(); i++) {
                //相同公司 相同姓名
                SysUser dbUser = listuser.get(i);
                String dbCompanyName = dbUser.getCompanyName() + "";
                String dbRealName = dbUser.getRealName() + "";
                if (dbRealName.equals(user.getUsername()) && (user.getCompanyName() + "").equals(dbCompanyName)) {
                    // 相同姓名 相同公司
                    user.setId(dbUser.getId());
                    return;
                } else if (dbRealName.equals(user.getUsername()) && !user.getCompanyName().equals(dbCompanyName)) {
                    // 相同姓名不同公司
                    count++;
                } else {
                    //不同姓名 公司相同、不同姓名不同公司
                    count++;
                }
            }
        }
        //生成账号
        SysUser newUser = new SysUser();
        //获取用户名
        String username = getUserName(user, count);
        newUser.setUsername(username);
        newUser.setCompanyName(user.getCompanyName());
        newUser.setPassword("12345678");
        newUser.setRealName(user.getUsername());
        newUser.setGxType(user.getGxType());
        newUser.setYwType(user.getYwType());
        newUser.setPersonType(user.getPersonType());
        newUser.setSource(user.getSource());
        newUser.setCreateDate(DateUtil.getCurrentDateTime());
        sysUserService.setUserFilePath(newUser);
        sysUserService.insertSelective(newUser);

        SysUserWorkExperience nowWork = user.getNowWork();
        if (nowWork != null) {
            sysUserService.setUserFilePath(newUser);
            nowWork.setIfNow("1");
            nowWork.setSuId(newUser.getId());
            userWorkExperienceService.insertSelective(nowWork);
        }

        user.setId(newUser.getId());
        user.setUsername(newUser.getUsername());
        user.setRealName(newUser.getRealName());

    }

    /**
     * @param user
     * @param count
     * @Author:zhaoyongke
     * @Description: 获取用户名
     * @Date:17:51 2020/12/31
     */
    public String getUserName(SysUser user, int count) {
        String userName = setUserName(user, count);
        int countUser = sysUserService.checkUser(userName);
        if (countUser != 0) {
            count = count + 1;
            userName = setUserName(user, count);
            int countUser1 = sysUserService.checkUser(userName);
            if (countUser1 > 0) {
                userName = getUserName(user, count);
            }
        }
        return userName;
    }

    /**
     * @param user
     * @param count
     * @Author:zhaoyongke
     * @Description:设置用户名
     * @Date:17:51 2020/12/31
     */
    public String setUserName(SysUser user, int count) {
        SysUser newUser = new SysUser();
        if (count > 0) {
            newUser.setUsername(user.getUsername() + "0" + count);
        } else {
            newUser.setUsername(user.getUsername());
        }
        return newUser.getUsername();
    }
    //给未中标人发消息
    private void sendNoticeNotZb(SupplierProjectAccept supa){
        SupplierProject sup=supplierProjectManagerService.selectByPrimaryKey(supa.getSupProjectId());
        ProjectNotice notice = new ProjectNotice();
        notice.setLinkId(supa.getSupProjectId());
        notice.setIsView("0");
        notice.setSendTo(supa.getAcceptBy());
        if(sup!=null&&!"".equals(sup)){
            notice.setTitle("很遗憾您应接的" + sup.getProjectName() + "项目未中标，请及时查看");
        }
        notice.setNoticeType("101");// 公示结束 发送消息
        notice.setApplicantType("002");// 供需
        notice.setCreateDate(DateUtil.getCurrentDateString());
        projectNoticeServie.insertSelective(notice);
    }

    /**
     * @param model
     * @param projectDTO
     * @Author:zhaoyongke
     * @Description:
     * @Date:17:28 2021/3/30
     */
    public void addModelDetail(Model model, SupplierProjectDTO projectDTO, HashMap  map,String userId) {
        String editurl = "projectManager/projectDetailEditNb";
        String detailurl = "projectManager/projectDetailXfNb";
        SysUser sysUser = getSysUser(userId);
        String auditStatus = sysUser.getAuditStatus();
        String createBy = projectDTO.getCreateBy();
        String publishBy = projectDTO.getPublishBy() + "";
        //到了截至日期就不可以应接
        Date currdate = new Date();
//        if(!"".equals(projectDTO.getZbEndDate()) || projectDTO.getZbEndDate() == null){
//            Date zbEndDate = DateUtil.parseDate(projectDTO.getZbEndDate(), "yyyy-MM-dd HH:mm");
//            if(currdate.after(zbEndDate)){
//                model.addAttribute("messages","招标截止日期已到不可以应接该项目");
//            }
//            if(currdate.before(zbEndDate)){
//                // 不到招标截止时间 不能选择 中标人（不显示选择按钮）
//                model.addAttribute("tbbut","0");
//
//            }
//        }
        //001 供方 002 需方
        String gxType = sysUser.getGxType();
        String sourse = sysUser.getSource();
        String zbStatus=projectDTO.getZbStatus();
        model.addAttribute("zbStatus",zbStatus);
        // 001  002 组织
        String personType = sysUser.getPersonType();
        Boolean isTs = false;
        // 001 正中人员  002  项目生成 --审核之后变成003  003自行注册
//        if ("001".equals(sourse)) {
//            model.addAttribute("source", "n");
//        } else {
//            model.addAttribute("source", "w");
//            if (!"1".equals(auditStatus)) {
//                isTs = true;
//                model.addAttribute("message", "暂时无法应接项目，需要完善信息才可以应接项目");
//            }
//        }

        if ("001".equals(gxType)) {
            //判断是否有迎接资格  外部注册的供方个人 （正中内部和供方组织不需要校验）
//            if ("001".equals(personType) && !isTs) {
//                JsonUtil acceptsjson = isAcceptZg(projectDTO, sysUser);
//                if (!acceptsjson.isFlag() && !isTs) {
//                    model.addAttribute("message", acceptsjson.getMsg());
//                }
//            }
//            model.addAttribute("gxtype", "g");
            SupplierProjectAccept accept = new SupplierProjectAccept();
            accept.setAcceptBy(userId);
            accept.setSupProjectId(projectDTO.getSupProjectId());
            int acceptcount = supplierProjectAcceptService.selectCount(accept);
            if (acceptcount > 0) {
                map.put("acceptview","1");
                //model.addAttribute("acceptview", "1");
            }

        } else if ("002".equals(gxType)) {
            // 控制综合对比
            // 判断是否有应接记录
            SupplierProjectAccept accept = new SupplierProjectAccept();
            accept.setSupProjectId(projectDTO.getSupProjectId());
            int acceptcount = supplierProjectAcceptService.selectCount(accept);
            if (acceptcount > 0 && (userId.equals(createBy) || userId.equals(publishBy))) {
                map.put("acceptview","1");
                //model.addAttribute("acceptview", "1");
            }
           // model.addAttribute("gxtype", "x");
        }
//        if (CommonUtil.getUserId().equals(createBy) || CommonUtil.getUserId().equals(publishBy)) {
//            model.addAttribute("myself", "1");
//        }
//        if ("001".equals(projectDTO.getPublishType())) {
//            //正中内部发布
//            if ("m".equals(type)) {
//                // 编辑页面
//                //如果项目oaId不为空,查询oa项目信息保存如model
//                if(StringUtils.isNotBlank(projectDTO.getOaId())){
//                    JSONObject paraObj = new JSONObject();
//                    paraObj.put("dataid",projectDTO.getOaId());
//                    try{
//                        OaData oaData1 = new OaData();
//                        oaData1.setOaId(projectDTO.getOaId());
//                        List<OaData> oaData = oaDataService.select(oaData1);
//                        if(null != oaData && CollectionUtil.isNotEmpty(oaData)){
//                            OaData oaReDataDTO = oaData.get(0);
//                            model.addAttribute("oaProjInfo", JSON.toJSONString(oaReDataDTO));
//                        }
//                    }catch (Exception e){
//                        e.printStackTrace();
//                    }
//                }
//            }
//            editurl = "projectManager/projectDetailEditNb";
//            detailurl = "projectManager/projectDetailXfNb";
//        } else {
//            editurl = "projectManager/projectDetailEditWb";
//            detailurl = "projectManager/projectDetailXfWb";
//
//        }
//        if ("m".equals(type)) {
//            // 编辑页面
//            return editurl;
//        } else {
//            //详情页面
//            return detailurl;
//        }

    }


    /**
     * @param linkId
     * @Author:zhaoyongke
     * @Description: 获取文件
     * @Date:18:12 2021/4/9
     */
    public void getFileModel(HashMap map, String linkId) {

        // 获取文件
        SupervisorFileTemp tempfile = new SupervisorFileTemp();
        tempfile.setLinkId(linkId);
        List<SupervisorFileTemp> selectfile = supervisorFileTempService.select(tempfile);
        Boolean zbsmfile = false;
        Boolean zbfile = false;
        Boolean tbfile = false;
        Boolean htfile = false;
        Boolean htsmfile = false;
        Boolean htzbtzs = false;
        // 311 上传项目招标文件说明文件
//        for (int i = 0; i < selectfile.size(); i++) {
//            SupervisorFileTemp supervisorFileTemp = selectfile.get(i);
//            String type1 = supervisorFileTemp.getType();
//            if ("311".equals(type1)) {
//                zbsmfile = true;
//                model.addAttribute("zbsmfile", zbsmfile);
//            }
//            if ("312".equals(type1)) {
//                zbfile = true;
//                model.addAttribute("zbfile", zbfile);
//            }
//            if ("313".equals(type1)) {
//                tbfile = true;
//                model.addAttribute("tbfile", tbfile);
//            }
//            if ("314".equals(type1)) {
//                htfile = true;
//                model.addAttribute("htfile", htfile);
//            }
//            if ("315".equals(type1)) {
//                //上传合同说明文件
//                htsmfile = true;
//                model.addAttribute("htsmfile", htsmfile);
//            }
//            if ("316".equals(type1)) {
//                //上传中标通知书
//                htzbtzs = true;
//                model.addAttribute("htzbtzs", htzbtzs);
//            }
//
//        }
        map.put("filelist", selectfile);
//        model.addAttribute("filelist", selectfile);
    }

    /**
     * @Author:zhaoyongke
     * @Description:
     * @Date:14:29 2021/3/24
     */
    public void getProjectPlace(HashMap map, SupplierProjectDTO projectDTO) {
        // 实施地点
        ProjectPlaceDTO dto = new ProjectPlaceDTO();
        dto.setSupProjectId(projectDTO.getSupProjectId());
        dto.setType("001");
        List<ProjectPlaceDTO> listpalce = projectPlaceService.selectPlace(dto);
//        for (int i = 0; i < listpalce.size(); i++) {
//            String province = listpalce.get(i).getProvince();
//            SysRegion  sysRegion=new SysRegion();
//            sysRegion.setCode(province);
//            List<SysRegion> sysRegionlist = sysRegionService.select(sysRegion);
//
//            listpalce.get(i).setCitys(citylist);
//            String city = listpalce.get(i).getCity();
//            List<SysRegion> countylist = sysRegionService.selectSysRegionByParentCode(city);
//            listpalce.get(i).setCountys(countylist);
//        }
//        projectDTO.setProjectPlacedtos(listpalce);
        map.put("listpalce", listpalce);

        // 人员地点
        ProjectPlaceDTO dtopersonplace = new ProjectPlaceDTO();
        dtopersonplace.setSupProjectId(projectDTO.getSupProjectId());
        dtopersonplace.setType("002");
        List<ProjectPlaceDTO> listPersonPalce = projectPlaceService.selectPlace(dtopersonplace);
//        for (int i = 0; i < listPersonPalce.size(); i++) {
//            String province = listPersonPalce.get(i).getProvince();
//            List<SysRegion> citylist = sysRegionService.selectSysRegionByParentCode(province);
//            listPersonPalce.get(i).setCitys(citylist);
//            String city = listPersonPalce.get(i).getCity();
//            List<SysRegion> countylist = sysRegionService.selectSysRegionByParentCode(city);
//            listPersonPalce.get(i).setCountys(countylist);
//        }
        projectDTO.setPersonPlacedtos(listPersonPalce);
        map.put("listperpalce", listPersonPalce);

    }


    /**
     * @Author:zhaoyongke
     * @Description:
     * @Date:14:25 2021/3/24
     */
    public void getProjectDic(HashMap map, SupplierProjectDTO dto) {
        // 学历类型
        List<BaseDict> edulist = baseDictService.selectByTypeCode("XL001");
        /*项目类型*/
        List<BaseDict> projectTypeList = DictUtil.getDictByType(DictTypes.YW_TYPE);
        /*行业类型*/
        List<BaseDict> hyTypeList = DictUtil.getDictByType(DictTypes.INDUSTRY);
        //擅长领域
        List<BaseDict> doBestList = baseDictService.selectByTypeCode(DictTypes.DOBEST.getType());
        // 省
        List<SysRegion> provincelist = sysRegionService.selectListByLevelId("1");

        if (StringUtil.isNotBlank(dto.getMainFzField())) {
            String mainFzFieldstr = getLxText(dto.getMainFzField(), DictTypes.DOBEST.getType());
//            model.addAttribute("strbest", mainFzFieldstr);
            map.put("strbest", mainFzFieldstr);
        }
        if (StringUtil.isNotBlank(dto.getProjectType())) {
            //項目類型
            String proTypestr = getLxText(dto.getProjectType(), DictTypes.YW_TYPE.getType());
            map.put("strprojecttype", proTypestr);
//            model.addAttribute("strprojecttype", proTypestr);
        }
        if (StringUtil.isNotBlank(dto.getHyType())) {
            //行业类型
            String hyTypestr = getLxText(dto.getHyType(), DictTypes.INDUSTRY.getType());
            map.put("hyTypestr", hyTypestr);
//            model.addAttribute("hyTypestr", hyTypestr);
        }
        // 市
        List<SysRegion> citylist = sysRegionService.selectSysRegionByParentCode(dto.getHtSignProvince());
        // 县
        List<SysRegion> countylist = sysRegionService.selectSysRegionByParentCode(dto.getHtSignCity());

        map.put("edulist", edulist);
        map.put("doBestList", doBestList);
//        model.addAttribute("citylist", citylist);
//        model.addAttribute("countylist", countylist);
//        model.addAttribute("projectTypeList", projectTypeList);
//        model.addAttribute("hytypelist", hyTypeList);
//        model.addAttribute("provincelist", provincelist);
//        model.addAttribute("edulist", edulist);
//        model.addAttribute("doBestList", doBestList);
    }

    /**
     * @param field
     * @param typecode
     * @Author:zhaoyongke
     * @Description:
     * @Date:15:04 2021/3/23
     */
    public String getLxText(String field, String typecode) {
        List<BaseDict> codeList = baseDictService.selectByTypeCode(typecode);
        HashMap<String, String> mapbest = new HashMap();
        for (int i = 0; i < codeList.size(); i++) {
            mapbest.put(codeList.get(i).getBadtCode(), codeList.get(i).getBadtName());
        }
        //擅长领域
        List<String> dobest = StringUtil.split(field);
        List<String> best = new ArrayList<>();
        for (int i = 0; i < dobest.size(); i++) {
            String s = dobest.get(i);
            String scname = mapbest.get(s);
            best.add(scname);
        }
        String strbest = best.toString().replaceAll("\\[", "").replaceAll("\\]", "");
        return strbest;

    }


//------------------------------------------------收藏--------------------------------------------------------------------

    /**
     * 查询我的收藏
     *
     * @param
     * @return
     */
    @PostMapping(value = "/selectCollect")
    @ResponseBody
    public JsonUtil selectCollect(@RequestBody SupplierProjectDTO project) {
        try {
            ProjectCollect projectCollect = new ProjectCollect();
            String userId = project.getUserId();
            if (StringUtils.isBlank(userId)) {
                return JsonUtil.error("获取参数失败");
            }
            projectCollect.setCreateBy(userId);
            List<ProjectCollect> list = pcService.selectCollect(projectCollect);


            return JsonUtil.sucessWithData("返回列表成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.error("获取失败");
        }

    }


    /**
     * 收藏
     *
     * @return
     */
    @PostMapping("insertCollect")
    @ResponseBody
    public JsonUtil insertCollect(@RequestBody SupplierProjectDTO dto) {
        JsonUtil json = new JsonUtil();
        String userId = dto.getUserId();
        String supProjectId = dto.getSupProjectId();
        if (StringUtils.isBlank(userId) || StringUtils.isBlank(supProjectId)) {
            json.setMsg("获取参数失败");
            json.setFlag(false);
            return json;
        }
        ProjectCollect co = new ProjectCollect();
        co.setProjectId(supProjectId);
        co.setCreateBy(userId);
        List<ProjectCollect> list = pcService.selectCollect(co);
        if (list != null && list.size() > 0) {
            json.setMsg("已经收藏过！");
            json.setFlag(false);
            return json;
        }
        co.setId(IdUtil.simpleUUID());
        co.setCreateDate(DateUtil.getCurrentDateTimeString());
        if (pcService.insertSelective(co) > 0) {
            json.setMsg("收藏成功");
            json.setFlag(true);
            return json;
        }
        json.setMsg("收藏失败");
        json.setFlag(false);
        return json;
    }

    /**
     * 取消收藏
     *
     * @param
     * @return
     */
    @PostMapping("deleteCollect")
    @ResponseBody
    public JsonUtil deleteCollect(@RequestBody SupplierProjectDTO dto) {
        try {
            JsonUtil json = new JsonUtil();
            Map map = new HashMap();

            String projectId = dto.getSupProjectId();
            String userId = dto.getUserId();
            if (StringUtils.isBlank(projectId) || StringUtils.isBlank(userId)) {
                return JsonUtil.error("获取参数失败");
            }

            ProjectCollect pc = new ProjectCollect();
            map.put("projectId", projectId);
            map.put("createBy", userId);
            pc.setProjectId(projectId);
            pc.setCreateBy(userId);
            List<ProjectCollect> list = pcService.selectCollect(pc);
            if (list == null || list.size() == 0) {
                json.setMsg("已取消过收藏");
                json.setFlag(false);
                return json;
            }
            if (pcService.deleteCollectApp(map) > 0) {
                json.setMsg("取消成功");
                json.setFlag(true);
                return json;
            }
            json.setMsg("取消失败");
            json.setFlag(false);
            return json;
        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.error("操作失败");
        }
    }


    /**
     * @param project
     * @Author:zhaoyongke
     * @Description:变更公告列表--- 所有项目的表更列表
     * @Date:15:09 2021/4/9
     */
    @ApiOperation(value = "/showChangeList", httpMethod = "GET", notes = "展示项目")
    @PostMapping(value = "/showChangeList")
    @ResponseBody
    public JsonUtil showChangeList(@RequestBody SupplierProjectDTO project) {
        try {
            SupplierProjectChange change = new SupplierProjectChange();
            change.setCheckStatus("2");
            List<SupplierProjectDTO> list = supplierProjectManagerService.selectProjectChangeListApp(project);
            return JsonUtil.sucessWithData("返回列表成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.error("获取失败");
        }


    }

    /**
     * @param supplierProjectDTO
     * @Author:zhaoyongke
     * @Description: 表更列表 ---- 该项目的变更列表
     * @Date:15:31 2021/10/18
     */
    @PostMapping(value = "/viewChangelist")
    @ResponseBody
    public JsonUtil viewChangelist(@RequestBody SupplierProjectDTO supplierProjectDTO) {
        try {
            if (StringUtils.isBlank(supplierProjectDTO.getSupProjectId())) {
                return JsonUtil.error("参数获取失败");
            }
            SupplierProjectChange change = new SupplierProjectChange();
            change.setSupProjectId(supplierProjectDTO.getSupProjectId());
            change.setCheckStatus("2");
            List<SupplierProjectChange> select = supplierProjectManagerChangeService.select(change);
            return JsonUtil.sucessWithData("返回列表成功", select);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.error("获取失败");
        }

    }

    /**
     * @param dto
     * @Author:zhaoyongke
     * @Description: 变更详情
     * @Date:16:20 2021/10/18
     */
    @PostMapping(value = "/viewChangeDetail")
    @ResponseBody
    public JsonUtil viewChangeDetail(@RequestBody SupplierProjectChangeDTO dto) {
        try {
            HashMap map = new HashMap();
            String supProjectChangeId = dto.getSupProjectChangeId();
            String userId = dto.getUserId();
            if (StringUtils.isBlank(supProjectChangeId) || StringUtils.isBlank(userId)) {
                return JsonUtil.error("参数获取失败");
            }
            String caseId=dto.getCaseId();
            if (StringUtil.isNotBlank(caseId)) {
                //修改提醒状态
                ProjectNotice projectNotice = projectNoticeServie.selectByPrimaryKey(caseId);
                projectNotice.setIsView("1");
                projectNoticeServie.updateByPrimaryKey(projectNotice);
            }
            HashMap changeDetail = getChangeDetail(supProjectChangeId, map);
            return JsonUtil.sucessWithData("返回数据成功", changeDetail);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.error("获取失败");
        }

    }

    public HashMap getChangeDetail(String supProjectChangeId, HashMap map) {

        SupplierProjectChange supplierProjectChange = supplierProjectManagerChangeService.selectByPrimaryKey(supProjectChangeId);
        String supProjectId = supplierProjectChange.getSupProjectId();
        SupplierProject project = supplierProjectManagerService.selectByPrimaryKey(supProjectId);
        if (StringUtil.isNotBlank(supplierProjectChange.getProjectType())) {
            //項目類型
            String proTypestr = getLxText(supplierProjectChange.getProjectType(), DictTypes.YW_TYPE.getType());
            map.put("strprojecttype", proTypestr);
        }
        String createBy = project.getCreateBy();
        String companyName = getFbdwByUser(createBy);
        SysUser sysUser1 = sysUserService.selectByPrimaryKey(createBy);
        map.put("user", sysUser1);
        map.put("project", project);
        map.put("companyName", companyName);
        map.put("change", supplierProjectChange);
        map.put("supProjectChangeId", supplierProjectChange.getSupProjectChangeId());
        // 查询修改记录
        List<SupervisorProjectModifiedRecord> tList = null;
        Page<AffairCaseDTO> tPage = PageHelper.startPage(0, 1000);
        SupervisorProjectModifiedRecord record = new SupervisorProjectModifiedRecord();
        record.setSpId(supProjectChangeId);
        try {
            List<SupervisorProjectModifiedRecord> list = modifiedRecordService.selectListByPage(record);
            map.put("list", list);
        } catch (MyException e) {
            e.printStackTrace();
        }
        return map;
    }


    /**
     * @param userId
     * @Author:zhaoyongke
     * @Description: 获取发布单位
     * @Date:10:24 2021/4/7
     */
    public String getFbdwByUser(String userId) {
        String companyName = "";
        SysUser sysUser = sysUserService.selectByPrimaryKey(userId);
        String source = sysUser.getSource();
        if ("001".equals(source)) {
            // 正中人员
            String companyId = sysUser.getCompanyName();
            BaseCompany baseCompany = baseCompanyService.selectByPrimaryKey(companyId);
            companyName = baseCompany.getBcName();
        } else {
            companyName = sysUser.getCompanyName();
        }
        return companyName;

    }

    /**
     * @param noticeDTO
     * @Author:zhaoyongke
     * @Description: 公示
     * @Date:16:19 2021/10/18
     */
    @ApiOperation(value = "/queryGsList", httpMethod = "GET", notes = "展示项目")
    @PostMapping(value = "/queryGsList")
    @ResponseBody
    public JsonUtil queryList(@RequestBody SupplierProjectPubNoticeDTO noticeDTO) {
        try {
            List<SupplierProjectPubNoticeDTO> list = supplierProjectPubNoticeService.selectGsListApp(noticeDTO);
            return JsonUtil.sucessWithData("返回数据成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.error("获取失败");
        }

    }

    /**
     * @param dto
     * @Author:zhaoyongke
     * @Description: 发布结果公示---需方发布
     * @Date:22:40 2021/4/6
     */
    @PostMapping(value = "viewPushGsDetail")
    @ResponseBody
    public JsonUtil viewPushGsDetail(@RequestBody SupplierProjectDTO dto) {
        try {
            JsonUtil jggs = getJggs(dto);
            return jggs;
        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.error("获取失败");
        }

    }

    /**
     * @param dto
     * @Author:zhaoyongke
     * @Description: 结果公示
     * @Date:10:01 2021/10/20
     */
    public JsonUtil getJggs(SupplierProjectDTO dto) {
        HashMap map = new HashMap();
        String supProjectId = dto.getSupProjectId();
        if (StringUtils.isBlank(supProjectId)) {
            return JsonUtil.error("获取参数失败");
        }
        SupplierProject project = supplierProjectManagerService.selectByPrimaryKey(supProjectId);
        SupplierProjectPubNoticeDTO noticedto = new SupplierProjectPubNoticeDTO();
        noticedto.setSupProjectId(supProjectId);
        List<SupplierProjectPubNotice> list = supplierProjectPubNoticeService.select(noticedto);
        if (list != null && list.size() > 0) {
            SupplierProjectPubNotice pubNotice = list.get(0);
            //中标单位
            String zbCompany = pubNotice.getZbCompany();
            String comType = pubNotice.getZbComType();
            //中标人
            String zbBy = pubNotice.getZbBy();
            String publish_by = project.getPublishBy();
            // 发布单位
            String pushcompany = getFbdwByUser(publish_by);
            //中标单位
            String zbcompany = getZbdwByCompanyId(zbCompany, comType);
            SysUser sysUser = sysUserService.selectByPrimaryKey(zbBy);
            //中标人
            String zbuserName = sysUser.getRealName();
            getUserJlZxJcManage(supProjectId, map);
            map.put("zbusername", zbuserName);
            map.put("zbcompany", zbcompany);
            map.put("pushcompany", pushcompany);
            map.put("supProjectId", supProjectId);
            map.put("project", project);
            map.put("pubNotice", pubNotice);

            String remark = project.getRemark();
            String publishType = project.getPublishType();
            String isConfirm = project.getIsConfirm();
            //
            if (!"001".equals(publishType)) {
                map.put("remark", remark);
                // model.addAttribute("remark",remark);
            } else if ("001".equals(publishType)) {
                if ("3".equals(isConfirm)) {
                    //
                    AffairRecord recordquerycheck = new AffairRecord();
                    recordquerycheck.setYwType("000");
                    recordquerycheck.setYwId(supProjectId);
                    List<AffairRecord> selectcheck = affairRecordService.select(recordquerycheck);
                    if (selectcheck != null) {
                        map.put("selectcheck", selectcheck);
                        //                    model.addAttribute("selectcheck",selectcheck);
                    }

                }

            }

        }


        return JsonUtil.sucessWithData("成功", map);
    }

    /**
     * @param companyId
     * @param comtype
     * @Author:zhaoyongke
     * @Description: 获取中标单位
     * @Date:10:31 2021/4/7
     */
    public String getZbdwByCompanyId(String companyId, String comtype) {
        String companyName = "";
        if ("001".equals(comtype)) {
            //001 保存的是公司表Id
            BaseCompany baseCompany = baseCompanyService.selectByPrimaryKey(companyId);
            companyName = baseCompany.getBcName();
        } else {
            SysUser sysUser = sysUserService.selectByPrimaryKey(companyId);
            companyName = sysUser.getCompanyName();
        }
        return companyName;

    }

    /**
     * @param supProjectId
     * @Author:zhaoyongke
     * @Description: 获取中标得项目经理名称
     * @Date:11:32 2021/9/6
     */
    public void getUserJlZxJcManage(String supProjectId, HashMap map) {

        //2021 0906  获取项目经理等信息
        SupplierProjectAccept notdto = new SupplierProjectAccept();
        notdto.setSupProjectId(supProjectId);
        notdto.setIsZb("1");
        List<SupplierProjectAccept> list = supplierProjectAcceptService.select(notdto);
        if (list != null) {
            if (list.size() > 0) {
                SupplierProjectAccept accept = list.get(0);
                if (accept != null) {
                    String jlManagerPerson = accept.getJlManagerPerson();
                    if (StringUtil.isNotBlank(jlManagerPerson)) {
                        SysUser sysUser1 = sysUserService.selectByPrimaryKey(jlManagerPerson);
                        if (sysUser1 != null) {
                            String jlmanagename = sysUser1.getUsername();
                            String realName = sysUser1.getRealName();
                            if (StringUtil.isNotBlank(realName)) {
                                map.put("jlmanagename", realName);
                                //  model.addAttribute("jlmanagename",realName);
                            } else {
                                map.put("jlmanagename", jlmanagename);
//                                model.addAttribute("jlmanagename",jlmanagename);
                            }

                        }
                    }
                    String jcManagerPerson = accept.getJcManagerPerson();
                    if (StringUtil.isNotBlank(jcManagerPerson)) {
                        SysUser sysUser1 = sysUserService.selectByPrimaryKey(jcManagerPerson);
                        if (sysUser1 != null) {
                            String jcmanagename = sysUser1.getUsername();
                            String realName = sysUser1.getRealName();
                            if (StringUtil.isNotBlank(realName)) {
                                map.put("jcmanagename", realName);
                                // model.addAttribute("jcmanagename",realName);
                            } else {
                                map.put("jcmanagename", jcmanagename);
                                // model.addAttribute("jcmanagename",jcmanagename);
                            }


                        }
                    }
                    String zxManagerPerson = accept.getZxManagerPerson();
                    if (StringUtil.isNotBlank(zxManagerPerson)) {
                        SysUser sysUser1 = sysUserService.selectByPrimaryKey(zxManagerPerson);
                        if (sysUser1 != null) {
                            String zxmanagename = sysUser1.getUsername();
                            String realName = sysUser1.getRealName();
                            if (StringUtil.isNotBlank(realName)) {

                                map.put("zxmanagename", realName);
                                //   model.addAttribute("zxmanagename",realName);
                            } else {
                                map.put("zxmanagename", zxmanagename);
                                // model.addAttribute("zxmanagename",zxmanagename);
                            }

                        }
                    }
                    String qtMagagerPerson = accept.getQtMagagerPerson();
                    if (StringUtil.isNotBlank(qtMagagerPerson)) {
                        SysUser sysUser1 = sysUserService.selectByPrimaryKey(qtMagagerPerson);
                        if (sysUser1 != null) {
                            String qtmanagename = sysUser1.getUsername();
                            String realName = sysUser1.getRealName();
                            if (StringUtil.isNotBlank(realName)) {
                                map.put("qtmanagename", realName);
//                                model.addAttribute("qtmanagename",realName);
                            } else {
                                map.put("qtmanagename", qtmanagename);
                                // model.addAttribute("qtmanagename",qtmanagename);
                            }

                        }
                    }
                }
                //

            }
        }
    }


    /**
     * @param supplierProjectDTO
     * @Author:zhaoyongke
     * @Description: 撤销应接
     * @Date:18:27 2021/3/27
     */
    @PostMapping("/updateCxAccept")
    @ResponseBody
    public JsonUtil updateCxAccept(@RequestBody SupplierProjectDTO supplierProjectDTO) {
        if (supplierProjectDTO != null) {
            try {
                JsonUtil re = new JsonUtil();
                String supProjectId = supplierProjectDTO.getSupProjectId();
                String userId = supplierProjectDTO.getUserId();
                if (StringUtils.isBlank(userId) || StringUtils.isBlank(supProjectId)) {
                    re.setFlag(false);
                    re.setMsg("获取参数失败");
                    return re;
                }
                SupplierProject project = supplierProjectManagerService.selectByPrimaryKey(supProjectId);
                //到了截至日期不可以撤销应接
                Date currdate = new Date();
                Date zbEndDate = DateUtil.parseDate(project.getZbEndDate(), "yyyy-MM-dd HH:mm");
                if (currdate.after(zbEndDate)) {
                    re.setMsg("招标截止日期已到不可以撤销应接");
                    re.setFlag(false);
                    return re;
                }
                if ("004".equals(project.getStatus())) {
                    re.setFlag(false);
                    re.setMsg("项目已结束，不能撤销应接");
                    return re;
                }
                SupplierProjectAccept accept = new SupplierProjectAccept();
                accept.setSupProjectId(supProjectId);
                accept.setAcceptBy(userId);
                List<SupplierProjectAccept> select = supplierProjectAcceptService.select(accept);
                for (int i = 0; i < select.size(); i++) {
                    // 刪除临时文件
                    SupervisorFileTemp supervisorFileTemp = new SupervisorFileTemp();
                    supervisorFileTemp.setLinkId(select.get(i).getSupAcceptId());
                    supervisorFileTempService.delete(supervisorFileTemp);
                    // 删除应接记录
                    supplierProjectAcceptService.deleteByPrimaryKey(select.get(i).getSupAcceptId());
                }
                re.setFlag(true);
                re.setMsg("操作成功");
                return re;
            } catch (Exception e) {
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return JsonUtil.error("操作失败:" + e.getMessage());
            }
        } else {
            return JsonUtil.error("获取数据失败！！");
        }

    }


    /*
     * 下载文件
     * */
    @GetMapping(value = "/exportFile")
    public void exportFile(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");// 文件id
        String url = "";
        if (!StringUtil.isBlank(id)) {
            SupervisorFileTemp supervisorFile = new SupervisorFileTemp();
            supervisorFile.setId(id);
            supervisorFile = supervisorFileTempService.selectByPrimaryKey(supervisorFile);
            url = supervisorFile.getUrl();
        }
        ExportFileUtil.exportFileApp(response, request, url);
    }

    /**
     * 压缩后下载
     *
     * @param request
     * @param response
     */
    @GetMapping(value = "/alldownload")
    public void Alldownload( HttpServletRequest request, HttpServletResponse response) {

        System.out.println("=================================");
        String projectId = request.getParameter("projectId");// 文件id
        if (StringUtils.isNotBlank(projectId)) {
            String dicName = "";
            String AsOutPath = tempPath + File.separator + dicName + DateUtil.getCurrentDateTimeStringNoSpliter() + ".zip";
            try {
                List<String> filePathList = new ArrayList<>();
                SupervisorFileTemp tempfile = new SupervisorFileTemp();
                tempfile.setLinkId(projectId);
                List<SupervisorFileTemp> selectfile = supervisorFileTempService.select(tempfile);
                for (SupervisorFileTemp file : selectfile) {
                    if (StringUtils.isNotBlank(file.getUrl())) {
                        filePathList.add(file.getUrl());
                    }
                }
                ZipUtils.compress(AsOutPath, filePathList);
            } finally {
            }
            ExportFileUtil.exportFileApp(response, request, AsOutPath);

        }
    }

    /**
    * @Author:zhaoyongke
    * @Description:
    * @param dto
    * @Date:15:21 2021/10/20
    */
    @PostMapping(value = "companyDetail")
    @ResponseBody
    public JsonUtil companyDetail(@RequestBody  SupplierProjectAcceptDTO dto) {
        HashMap  map=new HashMap();
        String supAcceptId = dto.getSupAcceptId();
        if(StringUtils.isBlank(supAcceptId)){
            return JsonUtil.error("获取参数失败");
        }
        SupplierProjectAccept accept = supplierProjectAcceptService.selectByPrimaryKey(supAcceptId);
        String acceptBy = accept.getAcceptBy();
        SysUser sysUser = sysUserService.selectByPrimaryKey(acceptBy);
        // 保存形式  001 （保存公司Id） 002 保存（应接人的Id）
        String companyId = accept.getCompanyId();
        String comType = accept.getComType();
        String companyName = "";
        String dwxz = "";
        String jgdm = "";
        String url="";
        if ("001".equals(comType)) {
            BaseCompany baseCompany = baseCompanyService.selectByPrimaryKey(companyId);
            companyName = baseCompany.getBcName();
            jgdm = baseCompany.getBcJgdm();
            String dwxzcode = baseCompany.getBcDwxz();
            dwxz=dwxzcode;
         /*   if ("001".equals(dwxzcode)) {
                dwxz = "企业";
            } else if ("002".equals(dwxzcode)) {
                dwxz = "机关";
            }*/
            url="projectManager/companyDetail";
        } else {
            companyName = sysUser.getCompanyName();
            String dwxzcode = sysUser.getCompanyType();
            if ("001".equals(dwxzcode)) {
                dwxz = "企业";
            } else if ("002".equals(dwxzcode)) {
                dwxz = "机关";
            }
            jgdm = sysUser.getJgcode();
            //公司业绩列表和证明文件
            SysUserProject userProject = new SysUserProject();
            userProject.setSuId(sysUser.getId());
            List<SysUserProject> userProjectList = sysUserProjectService.selectUserYj(userProject);
            map.put("userYj",userProjectList);
            //model.addAttribute("userYj",userProjectList);
            //公司资质证明文件
            SysUserCertificate userCertificate = new SysUserCertificate();
            userCertificate.setSuId(sysUser.getId());
            List<SysUserCertificate> sysUserCertificates = userCertificateService.selectUserCert(userCertificate);
            map.put("userZm",sysUserCertificates);
//            model.addAttribute("userZm",sysUserCertificates);
//            url="projectManager/companysDetail";
        }
        if (StringUtil.isNotBlank(sysUser.getDoBest())) {
            String mainFzFieldstr = getLxText(sysUser.getDoBest(), DictTypes.DOBEST.getType());
            map.put("strbest",mainFzFieldstr);
//            model.addAttribute("strbest", mainFzFieldstr);
        }

        // 学历类型
        List<BaseDict> edulist = baseDictService.selectByTypeCode("XL001");

        SupervisorFile supervisorFile = new SupervisorFile();
        supervisorFile.setProjectId(sysUser.getId());
        //supervisorFile.setType("101");
        supervisorFile = supervisorFileService.getPersonalImg(sysUser.getId());
        // 省
        List<SysRegion> provincelist = sysRegionService.selectListByLevelId("1");
        // 市
        List<SysRegion> citylist = sysRegionService.selectListByLevelId("2");


        map.put("edulist",edulist);
        map.put("fileuser",supervisorFile);
        map.put("companyName",companyName);
        map.put("dwxz",dwxz);
        map.put("jgdm",jgdm);
        map.put("user",sysUser);
        map.put("provincelist",provincelist);
        map.put("citylist",citylist);
        map.put("comType",comType);

        // 单位名称 // 单位性质  // 组织机构代码
//        model.addAttribute("edulist", edulist);
//        model.addAttribute("fileuser", supervisorFile);
//        model.addAttribute("companyName", companyName);
//        model.addAttribute("dwxz", dwxz);
//        model.addAttribute("jgdm", jgdm);
//        model.addAttribute("user", sysUser);
//        //省
//        model.addAttribute("provincelist", provincelist);
//        //市
//        model.addAttribute("citylist", citylist);

//        return JsonUtil.sucessWithData("返回数据成功", list);

        return JsonUtil.sucessWithData("成功", map);
    }


//    /**
//    * @Author:zhaoyongke
//    * @Description: 判断有没有应接记录
//    * @param supplierProjectDTO
//    * @Date:17:30 2021/10/19
//    */
//    @PostMapping("/isAccept")
//    @ResponseBody
//    public JsonUtil isAccept(@RequestBody SupplierProjectDTO supplierProjectDTO) {
//        try {
//            JsonUtil re = new JsonUtil();
//            String supProjectId = supplierProjectDTO.getSupProjectId();
//            String userId = supplierProjectDTO.getUserId();
//            Boolean isTs = false;
//            SupplierProjectAccept accept = new SupplierProjectAccept();
//            accept.setAcceptBy(userId);
//            accept.setSupProjectId(supProjectId);
//            int acceptcount = supplierProjectAcceptService.selectCount(accept);
//            if (acceptcount > 0) {
//                isTs = true;
//            }
//            if (isTs) {
//                re.setFlag(true);
//            } else {
//                re.setFlag(false);
//            }
//            return re;
//        } catch (Exception e) {
//            e.printStackTrace();
//            return JsonUtil.error("获取数据失败！！");
//        }
//    }



    /**
     * @param dto
     * @param model
     * @Author:zhaoyongke
     * @Description:
     * @Date:15:11 2021/4/10
     */
    @PostMapping(value = "viewUserDetail")
    @ResponseBody
    public JsonUtil viewUserDetail(@RequestBody  SupplierProjectDTO dto, Model model) {

        try {
            HashMap  map=new HashMap();
            String supProjectId = dto.getSupProjectId();
            SupplierProject project = supplierProjectManagerService.selectByPrimaryKey(supProjectId);
            if(StringUtil.isBlank(supProjectId)){
                JsonUtil.error("参数获取失败") ;
            }
            String zbBy = project.getZbBy();
            SupplierProjectAccept accept = new SupplierProjectAccept();
            accept.setAcceptBy(zbBy);
            accept.setSupProjectId(supProjectId);
            List<SupplierProjectAccept> select = supplierProjectAcceptService.select(accept);
            SupplierProjectAccept accept1 = select.get(0);
            SysUser userAccept = sysUserService.selectByPrimaryKey(accept1.getAcceptBy());
            String personType = userAccept.getPersonType();
            // 如果应接人是组织 查询联系人
            if("002".equals(personType)){
                userAccept.setRealName(userAccept.getLinkPerson());
            }
            map.put("userAccept",userAccept);
            String acceptComName = getAcceptdwByUser(accept1);
            String projectType = project.getProjectType();
            if (projectType.indexOf("001") > -1) {
                //监理
                String jlManagerPerson = accept1.getJlManagerPerson();
                SysUser sysUser = sysUserService.selectByPrimaryKey(jlManagerPerson);
                String realName = sysUser.getRealName();
                String personType1 = sysUser.getPersonType();
                if("002".equals(personType1)){
                    if(StringUtil.isBlank(realName)){
                        sysUser.setRealName(sysUser.getLinkPerson());
                    }
                }
                map.put("jlxmjl",sysUser);
            }
            if (projectType.indexOf("002") > -1) {
                //咨询
                String zxManagerPerson = accept1.getZxManagerPerson();
                SysUser sysUser = sysUserService.selectByPrimaryKey(zxManagerPerson);
                String realName = sysUser.getRealName();
                String personType1 = sysUser.getPersonType();
                if("002".equals(personType1)){
                    if(StringUtil.isBlank(realName)){
                        sysUser.setRealName(sysUser.getLinkPerson());
                    }
                }
                map.put("zxxmjl",sysUser);

            }
            if (projectType.indexOf("003") > -1) {
                //检测
                String jcManagerPerson = accept1.getJcManagerPerson();
                SysUser sysUser = sysUserService.selectByPrimaryKey(jcManagerPerson);
                String realName = sysUser.getRealName();
                String personType1 = sysUser.getPersonType();
                if("002".equals(personType1)){
                    if(StringUtil.isBlank(realName)){
                        sysUser.setRealName(sysUser.getLinkPerson());
                    }
                }
                map.put("jcxmjl",sysUser);
            }
            if (projectType.indexOf("100") > -1) {
                //其他
                String qtManagerPerson = accept1.getQtMagagerPerson();
                SysUser sysUser = sysUserService.selectByPrimaryKey(qtManagerPerson);
                String realName = sysUser.getRealName();
                String personType1 = sysUser.getPersonType();
                if("002".equals(personType1)){
                    if(StringUtil.isBlank(realName)){
                        sysUser.setRealName(sysUser.getLinkPerson());
                    }
                }
                map.put("qtxmjl",sysUser);
            }

            map.put("acceptComName",acceptComName);
            map.put("project",project);
            return JsonUtil.sucessWithData("成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonUtil.error("获取异常");
        }

    }
    /**
     * @Author:zhaoyongke
     * @Description: 获取应接单位
     * @Date:10:24 2021/4/7
     */
    public String getAcceptdwByUser(SupplierProjectAccept accept) {
        String acceptBy = accept.getAcceptBy();
        SysUser sysUser = sysUserService.selectByPrimaryKey(acceptBy);
        String comType = accept.getComType();
        String companyName = "";
        if ("001".equals(comType)) {
            BaseCompany baseCompany = baseCompanyService.selectByPrimaryKey(accept.getCompanyId());
            companyName = baseCompany.getBcName();

        } else {
            companyName = sysUser.getCompanyName();
        }
        return companyName;

    }

}
